package com.hdy.oa.web.action.department;

import com.hdy.DBUtils.JDBC;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/dept/details")
public class DepDetailsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

            out.print("     <!DOCTYPE html>");
            out.print("<html>");
            out.print("	<head>");
            out.print("		<meta charset='utf-8'>");
            out.print("		<title>部门详情</title>");
            out.print("	</head>");
            out.print("	<body>");
            out.print("		<h1 align='center'><span style='color: skyblue; '>部门详情</span></h1>");
            out.print("		<br>");

        String deptno = request.getParameter("deptNo");

        try {
            connection=JDBC.getConnection();
            String sql="select DEPTNO,DNAME,LOC from dept where DEPTNO=?";
            ps= connection.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();

            if(rs.next()){
                String deptNo=rs.getString("DEPTNO");
                String name=rs.getString("DNAME");
                String loc=rs.getString("LOC");

                out.print("		<div align='center'>");
                out.print("			");
                out.print("				<span style='color: purple; '>部门编号："+deptNo+"</span> <br>");
                out.print("				<span style='color: purple; '>部门名称："+name+" </span><br>");
                out.print("				<span style='color: purple; '>部门位置："+loc+" </span><br>");
                out.print("			");
                out.print("		</div>");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps, rs);
        }
        out.print("		<br>");
        out.print("		<hr>");
        out.print("		<div align='center'>");
        out.print("			<input  type='button' value='返回' onclick='window.history.back()'>");
        out.print("		</div>");
        out.print("		");
        out.print("	</body>");
        out.print("</html>");
    }
}
